﻿using HR60_PanelSoft.Model;
using HR60_PanelSoft.NetWork.Base;
using HR60_PanelSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.NetWork.Sender
{
    class Sender_0103:BaseSender
    {
        public Sender_0103(byte panelNo, byte sceneNum, Scene scene, Device device)
        {
            byte[] data = new byte[48]; //12个分机


            List<Device> listDevice = device.childDevices;
            int cur = 0;
            foreach (Device cd in listDevice)
            {

                SceneEvent se = scene.findEventByDevice(cd.uuid);
                //1.无
                if (se == null)
                {
                    cur = cur + 4;
                    continue;
                }

                List<string> sArrts = se.attrs;

                List<string> values = se.attrValueKeys;


                DeviceType type = cd.type;
                List<CtrlAttr> attrs = type.attrs;
                if (attrs == null)
                {
                    cur = cur + 4;
                    continue;
                }

                if (attrs.Count != 5)
                {
                    throw new Exception("数据内容有问题,请查看背景音乐属性是否配置正确？");
                }


                if (attrs.Count == 5)
                {

                    string binaryString = "1";//待拼接的字符串

                    {
                        {

                            CtrlAttr attr0 = attrs[0];
                            int index = sArrts.IndexOf(attr0.name);
                            string value0 = null;
                            if (index != -1)
                            {
                                value0 = values[index];
                            }


                            if (value0 == null)  //开关机
                            {
                                binaryString = binaryString + "0";
                            }
                            else
                            {
                                binaryString = binaryString + value0;
                            }

                        }
                    }

                  

                    

                    //2
                    {

                        CtrlAttr attr1 = attrs[1];
                        int index = sArrts.IndexOf(attr1.name);
                        string value1 = null;
                        if (index != -1)
                        {
                            value1 = values[index];
                        }

                        if (value1 == null)
                        {
                            binaryString = binaryString + "0";
                        }
                        else if (value1 == "0")
                        {
                            binaryString = binaryString + "0";
                        }
                        else if (value1 == "1")
                        {
                            binaryString = binaryString + "1";
                        }

                    }

                  

                    binaryString = binaryString + "00";// 填充


                    //3

                    {


                        CtrlAttr attr2 = attrs[2];//风速
                        int index = sArrts.IndexOf(attr2.name);
                        string value2 = null;
                        if (index != -1)
                        {
                            value2 = values[index];
                        }

                        if (value2 == null)
                        {
                            binaryString = binaryString + "000";
                        }
                        else if (value2 == "0")
                        {
                            binaryString = binaryString + "000";
                        }
                        else if (value2 == "1")
                        {
                            binaryString = binaryString + "001";
                        }
                        else if (value2 == "2")
                        {
                            binaryString = binaryString + "010";
                        }
                        else if (value2 == "3")
                        {
                            binaryString = binaryString + "011";
                        }
                        else if (value2 == "4")
                        {
                            binaryString = binaryString + "100";
                        }
                        else if (value2 == "5")
                        {
                            binaryString = binaryString + "101";
                        }

                    }

                 
                    data[cur++] = Convert.ToByte(binaryString, 2);

                    //4
                    {

                        CtrlAttr attr3 = attrs[3];
                        int index = sArrts.IndexOf(attr3.name);
                        string value3 = null;
                        if (index != -1)
                        {
                            value3 = values[index];
                        }

                        if (value3 == null)
                        {
                            data[cur++] = 0;
                        }
                        else
                        {
                            data[cur++] = Convert.ToByte(value3, 10);
                        }
                    }

                   

                    //5


                    {

                        CtrlAttr attr4 = attrs[4];
                        int index = sArrts.IndexOf(attr4.name);
                        string value4 = null;
                        if (index != -1)
                        {
                            value4 = values[index];
                        }
                        if (value4 == null)
                        {
                            data[cur++] = 0;
                        }
                        else
                        {
                            data[cur++] = Convert.ToByte(value4, 10);
                        }
                    }
                   

                   
                   
                    data[cur++] = 0;
                }


            }

            //构造package包
            this.package = Package.getPackage(0x01, 0x03, panelNo, sceneNum, data);

        }
    }
}
